package com.ruoyi.app.service.impl;

import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.app.mapper.AppCreditRulesMapper;
import com.ruoyi.app.domain.AppCreditRules;
import com.ruoyi.app.service.IAppCreditRulesService;

/**
 * 学分规则Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-19
 */
@Service
public class AppCreditRulesServiceImpl extends ServiceImpl<AppCreditRulesMapper,AppCreditRules> implements IAppCreditRulesService
{
    @Autowired
    private AppCreditRulesMapper appCreditRulesMapper;
    /**
     * 查询学分规则
     *
     * @param ruleId 学分规则主键
     * @return 学分规则
     */
    @Override
    public AppCreditRules selectAppCreditRulesByRuleId(Long ruleId)
    {
        AppCreditRules entity = appCreditRulesMapper.selectById(ruleId);
        return entity;
    }

    /**
     * 查询学分规则列表
     *
     * @param appCreditRules 学分规则
     * @return 学分规则
     */
    @Override
    public List<AppCreditRules> selectAppCreditRulesList(AppCreditRules appCreditRules)
    {
        LambdaQueryWrapper<AppCreditRules> lqw = buildQueryWrapper(appCreditRules);
        return appCreditRulesMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<AppCreditRules> buildQueryWrapper(AppCreditRules appCreditRules) {
        Map<String, Object> params = appCreditRules.getParams();
        LambdaQueryWrapper<AppCreditRules> lqw = Wrappers.lambdaQuery();
                    lqw.like(StringUtils.isNotBlank(appCreditRules.getRuleName()), AppCreditRules::getRuleName, appCreditRules.getRuleName());
                    lqw.eq(StringUtils.isNotBlank(appCreditRules.getDescription()), AppCreditRules::getDescription, appCreditRules.getDescription());
                    lqw.eq(appCreditRules.getPoints() != null, AppCreditRules::getPoints, appCreditRules.getPoints());
        return lqw;
    }
    /**
     * 新增学分规则
     *
     * @param appCreditRules 学分规则
     * @return 结果
     */
    @Override
    public int insertAppCreditRules(AppCreditRules appCreditRules)
    {
                appCreditRules.setCreateBy(SecurityUtils.getUsername());
                appCreditRules.setCreateTime(DateUtils.getNowDate());
            return appCreditRulesMapper.insert(appCreditRules);
    }

    /**
     * 修改学分规则
     *
     * @param appCreditRules 学分规则
     * @return 结果
     */
    @Override
    public int updateAppCreditRules(AppCreditRules appCreditRules)
    {
                appCreditRules.setUpdateBy(SecurityUtils.getUsername());
                appCreditRules.setUpdateTime(DateUtils.getNowDate());
        return appCreditRulesMapper.updateById(appCreditRules);
    }

    /**
     * 批量删除学分规则
     *
     * @param ruleIds 需要删除的学分规则主键
     * @return 结果
     */
    @Override
    public int deleteAppCreditRulesByRuleIds(Long[] ruleIds)
    {
        return appCreditRulesMapper.deleteBatchIds(Arrays.asList(ruleIds));
    }

    /**
     * 删除学分规则信息
     *
     * @param ruleId 学分规则主键
     * @return 结果
     */
    @Override
    public int deleteAppCreditRulesByRuleId(Long ruleId)
    {
        return appCreditRulesMapper.deleteById(ruleId);
    }
}
